package com.easylinkin.linkappapi.grid.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.grid.entity.GridInfo;
import com.easylinkin.linkappapi.grid.enums.EGridFileBusSubDef;
import com.easylinkin.linkappapi.grid.enums.EGridFileBusType;
import com.easylinkin.linkappapi.grid.mapper.GridHiddenDangerMapper;
import com.easylinkin.linkappapi.grid.models.query.GridFileBatchQuery;
import com.easylinkin.linkappapi.grid.models.query.GridFileQuery;
import com.easylinkin.linkappapi.grid.models.query.GridHiddenDangerQuery;
import com.easylinkin.linkappapi.grid.models.vo.GridFileInfoVO;
import com.easylinkin.linkappapi.grid.models.vo.GridHiddenDangerInfoVO;
import com.easylinkin.linkappapi.grid.models.vo.GridHiddenDangerListVO;
import com.easylinkin.linkappapi.grid.service.IGridFileService;
import com.easylinkin.linkappapi.grid.service.IGridHiddenDangerService;
import com.easylinkin.linkappapi.grid.service.IGridInfoService;
import com.easylinkin.linkappapi.safe.dto.HiddenDangerDTO;
import com.easylinkin.linkappapi.safe.entity.HiddenDanger;
import com.easylinkin.linkappapi.safe.service.HiddenDangerService;
import com.easylinkin.linkappapi.security.entity.LinkappUser;
import com.easylinkin.linkappapi.security.service.LinkappUserService;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author kyj
 * @date 2024/5/27 16:03
 */
@Slf4j
@Setter(onMethod_ = @Resource)
@Validated
@Service
public class GridHiddenDangerService extends ServiceImpl<GridHiddenDangerMapper, HiddenDanger> implements IGridHiddenDangerService {

    private HiddenDangerService hiddenDangerService;
    private IGridInfoService gridInfoService;
    private LinkappUserService linkappUserService;
    private IGridFileService gridFileService;

    @Override
    public HiddenDangerDTO saveOne(HiddenDanger hiddenDanger) {
        return hiddenDangerService.saveOne(hiddenDanger);
    }

    @Override
    public IPage<GridHiddenDangerListVO> pageList(IPage<?> page, GridHiddenDangerQuery query) {
        IPage<GridHiddenDangerListVO> pageResult = baseMapper.pageList(page, query);
        List<GridHiddenDangerListVO> records = pageResult.getRecords();
        Set<Integer> gridIds = records.stream().map(GridHiddenDangerListVO::getGridId).collect(Collectors.toSet());
        Map<Integer, GridInfo> gridInfoMap = gridInfoService.mapByIds(gridIds);

        Set<String> createUids = records.stream().filter(record -> ObjectUtil.isNotNull(record.getCreateUid())).map(record -> String.valueOf(record.getCreateUid())).collect(Collectors.toSet());
        Map<String, LinkappUser> userMap = linkappUserService.mapUserByIds(createUids);

        Set<String> hiddenDangerIds = records.stream().map(GridHiddenDangerListVO::getId).collect(Collectors.toSet());
        Map<String, List<GridFileInfoVO>> fileInfoListMap;
        if(CollUtil.isNotEmpty(hiddenDangerIds)){
            fileInfoListMap  = gridFileService.mapListInfoByBatchQuery(GridFileBatchQuery.builder()
                    .busIds(hiddenDangerIds)
                    .busType(EGridFileBusType.HIDDEN_DANGER.getCode())
                    .build()
            );
        } else {
            fileInfoListMap = null;
        }


        records.parallelStream().forEach(record -> {
            if(ObjectUtil.isNotNull(gridInfoMap)){
                GridInfo gridInfo = gridInfoMap.get(record.getGridId());
                if (ObjectUtil.isNotNull(gridInfo)) {
                    record.setGridName(gridInfo.getGridName());
                }
            }
            if(ObjectUtil.isNotNull(userMap)){
                LinkappUser linkappUser = userMap.get(String.valueOf(record.getCreateUid()));
                if (ObjectUtil.isNotNull(linkappUser)) {
                    record.setCreator(linkappUser.getNickname());
                    record.setCreatePhone(linkappUser.getPhone());
                }
            }

            String fileKey = record.getId() + "," + EGridFileBusType.HIDDEN_DANGER.getCode() + "," + EGridFileBusSubDef.DEF.getCode();
            if(ObjectUtil.isNotNull(fileInfoListMap)){
                List<GridFileInfoVO> fileInfoList = fileInfoListMap.get(fileKey);
                if (CollUtil.isNotEmpty(fileInfoList)) {
                    record.setRectifyVoice(fileInfoList.get(0));
                }
            }

        });

        return pageResult;
    }

    @Override
    public GridHiddenDangerInfoVO getOneById(Serializable id) {
        HiddenDangerDTO hiddenDangerDTO = hiddenDangerService.getOneById(id);

        if (ObjectUtil.isNull(hiddenDangerDTO)) {
            return null;
        }

        GridHiddenDangerInfoVO data = new GridHiddenDangerInfoVO(hiddenDangerDTO);

        // 网格数据
        Set<Integer> gridIds = Collections.singleton(data.getGridId());
        Map<Integer, GridInfo> gridInfoMap = gridInfoService.mapByIds(gridIds);
        GridInfo gridInfo = gridInfoMap.get(data.getGridId());
        if (ObjectUtil.isNotNull(gridInfo)) {
            data.setGridName(gridInfo.getGridName());
        }

        // 创建人数据
        Set<String> createUids = Collections.singleton(String.valueOf(data.getCreateUid()));
        Map<String, LinkappUser> userMap = linkappUserService.mapUserByIds(createUids);
        LinkappUser linkappUser = userMap.get(String.valueOf(data.getCreateUid()));
        if (ObjectUtil.isNotNull(linkappUser)) {
            data.setCreator(linkappUser.getNickname());
            data.setCreatePhone(linkappUser.getPhone());
        }

        // 反馈语音
        List<GridFileInfoVO> fileInfoList = gridFileService.listInfoByQuery(GridFileQuery.builder()
                .busId(id)
                .busType(EGridFileBusType.HIDDEN_DANGER.getCode())
                .busSubType(EGridFileBusSubDef.DEF.getCode())
                .build());
        if (CollUtil.isNotEmpty(fileInfoList)) {
            data.setRectifyVoice(fileInfoList.get(0));
        }

        return data;
    }
}
